package cn.zh.advisor.config;

import cn.zh.advisor.anno.TargetDataSource;
import cn.zh.advisor.ds.DynamicDataSourceAnnotationAdvisor;
import cn.zh.advisor.ds.DynamicDataSourceAnnotationInterceptor;
import org.springframework.aop.Advisor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;

/**
 * @author zh
 * 仿动态数据源依赖@DS实现（复制三个类）
 */
@Configuration
public class AdvisorConfig {

    @Bean
    /**
     * Advisor会被InfrastructureAdvisorAutoProxyCreator会将所有Advisor扫描，再将被这个注解标明Advisor添加到候选Advisor中
     * 即加入到bean初始化后去匹配的Advisor列表中
     */
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public Advisor dsAdvisor(){
        DynamicDataSourceAnnotationInterceptor interceptor = new DynamicDataSourceAnnotationInterceptor(true);
        DynamicDataSourceAnnotationAdvisor advisor = new DynamicDataSourceAnnotationAdvisor(interceptor, TargetDataSource.class);
        advisor.setOrder(Integer.MIN_VALUE);
        return advisor;
    }
}
